﻿@model Roulette.Models.TableModel

@{
    ViewBag.Title = "Roulette Admin";
}

@section Head
{
     <script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
     <script type="text/javascript" src="/Scripts/jquery-ui-1.8.11.js"></script>
     <link rel="stylesheet" href="http://code.jquery.com/ui/1.8.21/themes/base/jquery-ui.css" type="text/css" media="all" />
     <script type="text/javascript" src="/Scripts/Custom/highlighter.js"></script>
     <script type="text/javascript" src="/Scripts/Custom/stake.js?wd=15"></script>
     <script type="text/javascript" src="/Scripts/json2.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.signalR-0.5.0.min.js"></script>
    <script type="text/javascript" src="/signalr/hubs"></script>
     <script type="text/javascript">
         var checkSum;
         var contractNumber = 0;
         var NUMBERS = [26, 3, 35, 12, 28, 7, 29, 18, 22, 9, 31, 14, 20, 1, 33, 16, 24, 5, 10, 23, 8, 30, 11, 36, 13, 27, 6, 34, 17, 25, 2, 21, 4, 19, 15, 32, 0];

         function setRoundNumber() {
             setTimeout(function () {
                 getRoundNumber();
                 setRoundNumber();
             }, 2000);
         }

         

        var questions = $.connection.questionsHub;
        questions.newQuestion = function (data) {
            drawStakes(data);
         };

         function drawStakes(stakes) {
            if (stakes == "") {
                $('.roulette-board div.chip').remove();
                $('#zero div.chip').remove();
                stakes = new Array();
                checkSum.val("0");
            }
        }


         $(function () {
             $.connection.hub.start();
             $.ajax({
                 type: "POST",
                 url: '/Stake/RememberCurrentState',
                 data: { currentState: "" }
             });
             checkSum = $("#check-sum");
             $(".datetext").datepicker();
             $('#check-dialog, #pay-dialog').dialog({
                 modal: true,
                 autoOpen: false,
                 width: 235,
                 height: 200,
                 title: "Check code",
                 open: function (event, ui) {

                 },
                 close: function (event, ui) {
                 }
             });

             $('#report-dialog').dialog({
                 modal: true,
                 autoOpen: false,
                 width: 950,
                 height: 600,
                 title: "Report",
                 open: function (event, ui) {
                     $('#report-dialog #endDate').val(getDateString(new Date()));
                     var lastWeek = new Date();
                     lastWeek.setDate(lastWeek.getDate() - 1);
                     $('#report-dialog #startDate').val(getDateString(lastWeek));
                 },
                 close: function (event, ui) {
                     $("#reports").empty();
                 }
             });

             $("#pay-dialog").dialog({ title: "Pay Winners" });
             $("#show").click(function () {
                 $("#reports").load('@Url.Action("Report","Stake")', { startDate: $(this).siblings("#startDate").eq(0).val(), endDate: $(this).siblings("#endDate").eq(0).val() }, function () { });
             });
             $("#check-button").click(function () {
                 $("#check-dialog").dialog("open");
             });

             $("#check-botton-dialog").click(function () {
                 $.post("/Stake/CheckWinner", { barCode: $(".code-textbox").val() }, function (data) {
                     if (data.isWinner) {
                         $("#hiddenTextBox").val($(".code-textbox").val());
                         $("#pay").show();
                     }
                     else {
                         $("#pay").hide();
                     }
                     $(this).parent().parent().dialog("close");
                     $("#paySum").html(data.winning);
                     $(".code-textbox").val("");
                     $("#pay-dialog").dialog("open");

                 });
             });

             $(".cancel-botton-dialog").click(function () {
                 $(this).parent().parent().dialog("close");
             });

             $("#pay").click(function () {
                 $.post("/Stake/PayWinner", { barCode: $("#hiddenTextBox").val() }, function (data) {
                     if (data.isPayed) {
                         alert("Ok");
                     }
                     else {
                         alert("Error according when executing this query. Please try later.");
                     }
                 });

                 $(this).parent().parent().dialog("close");
             });

             $("#report-button").click(function () { $("#report-dialog").dialog("open"); });

             $("#cancel-button").click(function () {
                 checkSum.val("0");
                 $.ajax({
                     type: "POST",
                     url: '/Stake/RememberCurrentState',
                     data: { currentState: "" },
                     success: function () {
                         stakes = new Array();
                         $('.roulette-board div.chip').remove();
                         $('#zero div.chip').remove();
                     }
                 });
             });

             $("#cancel-any-button").click(function () {
                 if ($(this).hasClass("clicked")) {
                     $(this).removeClass("clicked");
                     $(".push-item").css("cursor", "pointer");
                     $("div.round").css("cursor", "pointer");
                     if ($("#complect-button").hasClass("clicked")) {
                         $(".push-item").css("cursor", "move");
                         $("div.round").css("cursor", "move");
                         $("#centered-div .chip").css("cursor", "move");
                     }
                 }
                 else {
                     $(this).addClass("clicked");
                     $(".push-item").css("cursor", "url('/img/delete-cursor.png') 12 12,pointer");
                     $("div.round").css("cursor", "url('/img/delete-cursor.png') 12 12,pointer");
                     $("#centered-div .chip").css("cursor", "url('/img/delete-cursor.png') 12 12,pointer");
                 }
             });

             $("#submit-button").click(function () {
                 if (!isFormValid) {
                     alert("Cтавка может состоять только из положительных чисел");
                     return;
                 }
                 $.ajax({
                     type: "POST",
                     url: '/Stake/CreateStake',
                     //data: JSON.stringify(stakes),
                     data: { stake: JSON.stringify(stakes), state: $("#centered-div").html() },
                     async: false,
                     success: function (data) {
                         if (data.success) {
                             stakes = new Array();
                             checkSum.val("0");
                             $('.roulette-board div.chip').remove();
                             $('#zero div.chip').remove();
                             contractNumber = data.contractNumber;
                             PrintCheck(contractNumber);
                         } else {
                             alert("Произошла ошибка, повторите ставку");
                         }
                     }
                 });
             });


             $("#complect-button").click(function () {
                 if ($(this).hasClass("clicked")) {
                     $(this).removeClass("clicked");
                     $(".push-item").css("cursor", "pointer");
                     $("div.round").css("cursor", "pointer");
                     if ($("#cancel-any-button").hasClass("clicked")) {
                         $(".push-item").css("cursor", "url('/img/delete-cursor.png') 12 12,pointer");
                         $("div.round").css("cursor", "url('/img/delete-cursor.png') 12 12,pointer");
                         $("#centered-div .chip").css("cursor", "url('/img/delete-cursor.png') 12 12,pointer");
                     }
                 } else {
                     $(this).addClass("clicked");
                     if (!$("#cancel-any-button").hasClass("clicked")) {
                         $(".push-item").css("cursor", "move");
                         $("div.round").css("cursor", "move");
                         $("#centered-div .chip").css("cursor", "move");
                     }
                 }
             });

             currentRoundNumber = @ViewBag.CurrentRound;
             $("#round-number").html(@ViewBag.CurrentRound);

             setRoundNumber();
             setProgressBar();
         });

         function getDateString(date) {
             var dd = date.getDate();
             var mm = date.getMonth() + 1; //January is 0!
             var yyyy = date.getFullYear();
             if (dd < 10) { dd = '0' + dd }
             if (mm < 10) { mm = '0' + mm }
             return mm + '/' + dd + '/' + yyyy;
         }

         function PrintCheck(contractNumber) {
             if (contractNumber != 0) {
                 window.open("/Stake/Check?contractNumber=" + contractNumber, "_blank");
             }
         }
     </script>
}

@section seperator
{    
    @if (Roles.IsUserInRole("SystemAdmin") || Roles.IsUserInRole("Admin"))
    {
        <a style="float:right; margin: 4px 30px 0 0; font-size: 14px; color: #fff" href="/Adminpanel">Adminpanel</a>
    }
    <a style="float:right; margin: 4px 30px 0 0; font-size: 14px; color: #fff" href="/Account/Logout">Log out</a>
}

<div id="admin-board">

    <div id="buttons">
        <input type="text" id="check-sum" name="check-sum" value="0" readonly="readonly" />

        <input type="button" value="Удалить фишку" id="cancel-any-button" class="admin-button"/>
        <input type="button" value="Удалить все" id="cancel-button" class="admin-button"/>
        <input type="button" value="Ставить " id="submit-button" class="admin-button" />
        <input type="button" value="Проверить " id="check-button" class="admin-button" />
        <input type="button" value="Oтчет" id="report-button" class="admin-button" />
        <input type="button" value="Комплект" id="complect-button" class="admin-button" />
    </div>
    <div id="centered-div">
        @Html.Partial("_Board", Model)
    </div>
    <div id="sectors-div" style="margin-top: 40px;">
        @Html.Partial("_Sectors", true)
    </div>

    <div class="roulette-chips">
        <div class="chip" data-value="50">
            <img src="/img/green-chip.png" alt="50"/>
            <div class="text">50</div>
        </div>
        <div class="chip" data-value="100">
            <img src="/img/purple-chip.png" alt="100"/>
            <div class="text">100</div>
        </div>
        <div class="chip" data-value="250">
            <img src="/img/blue-chip.png" alt="250"/>
            <div class="text">250</div>
        </div>
        <div class="chip" data-value="500">
            <img src="/img/orange-chip.png" alt="500"/>
            <div class="text">500</div>
        </div>
        <div class="chip" data-value="1000">
            <img src="/img/red-chip.png" alt="1000"/>
            <div class="text">1000</div>
        </div>
         <div class="chip" data-value="3000">
            <img src="/img/yellow-chip.png" alt="3000"/>
            <div class="text">3000</div>
        </div>
        <div class="chip" data-value="5000">
            <img src="/img/dark-chip.png" alt="5000"/>
            <div class="text">5000</div>
        </div>
    </div>
    <div style="margin-top: 5px"><input class="bet" type="text" value="" /></div>
</div>
<div id="check-dialog">
 <input type="text" class="code-textbox" />
 <div class="check-dialog-buttons">
     <input type="button" value="Check" class="admin-button" id="check-botton-dialog"/>
     <input type="button" value="Cancel" class="admin-button cancel-botton-dialog" />
 </div>
</div>

<div id="pay-dialog">
    <label id="paySum"></label>
    <input type="hidden" id="hiddenTextBox" />
    <div class="check-dialog-buttons">
        <input type="button" value="Pay" class="admin-button" id="pay"/>
        <input type="button"value="Cancel" class="admin-button cancel-botton-dialog" />
    </div>
</div>

<div id="report-dialog">
    <label class="date">Дата начала отчета:</label>
    <input type="text" id = "startDate" class="datetext" />
    <label class="date"> Дата окончания отчета:</label>
    <input type="text" id="endDate" class="datetext" />
    <input type="button" value="Показать" class="admin-button" id="show"/>
    <div id="reports"></div>
</div>